var CreateXfsqController = null;
var XfsqConstants = {"voucherIndex":""};
(function(doc, win) {
	
 
    
    // sui 页面就绪时间
    $(doc).on("pageInit", "#createXfsqActivityPage", function(e, pageId, $page,isBack) {   	
    	closePageWindow("createXfsqActivityPage");
    	if(isBack){
    		var paramObj = CommonUtil.takeOutParamObject(); 
        	if(paramObj && paramObj.createActivityParams!=undefined){
        		createActivityPublicParam = paramObj.createActivityParams;
        	}			
        	return;
		}
    	//test代码;
    	var param = {
    		 
    		"activityType" : "35"
    		 
    	};
  	    ActivityPreloadData.clearCreateActivityParam();
        CommonUtil.injectParamObject(param);
        createXfsqController = new CreateXfsqController();
    	createXfsqController.initPageData();
    	  	
    });
  
    
    /**
     * 下一步;
     */
    $(doc).on("click", "#createXfsqActivityPage #nextBtn", function() {      
    	if(createXfsqController.checkVoucherData() == true){
    		var paramObj = CommonUtil.takeOutParamObject();     	 
            if(paramObj.isFristRoute==undefined){
        		 CommonUtil.injectParamObject({"activityType":ACTIVITY_TYPE.LATTER_SEND.val,"reload":true,"createActivityParams":createActivityPublicParam});
        		 $.router.load("create_activity_xhyzm.html",true);
        	}else{
        		 CommonUtil.injectParamObject({"activityType":ACTIVITY_TYPE.LATTER_SEND.val,"reload":false,"createActivityParams":createActivityPublicParam});
        		 $.router.load("create_activity_xhyzm.html");
        	}
        
    	}
    	 
    });
 

    /**
     * 返回事件;
     */
    $(doc).on("click","#createXfsqActivityPage .back",function(){
    	  ActivityPreloadData.clearCreateActivityParam();
    });
    
  
    $(doc).on('input', '#createXfsqActivityPage #receiveConditionMoney,#voucherAmount,#voucherMinConsumeAmount', function (e) {
    	 var receiveConditionMoney = $("#createXfsqActivityPage #receiveConditionMoney").val();
    	 var voucherAmount =$("#createXfsqActivityPage #voucherAmount").val();
    	 var voucherMinConsumeAmount =$("#createXfsqActivityPage #voucherMinConsumeAmount").val(); 
    	 if(receiveConditionMoney!="" && voucherAmount!="" &&voucherMinConsumeAmount!=""){
    		 $("#createXfsqActivityPage #quan-sure").removeClass("disabled");
    	 }else{
    		 if($("#createXfsqActivityPage #quan-sure").attr("class").indexOf("disabled")==-1){
    			 $("#createXfsqActivityPage #quan-sure").addClass("disabled");
    		 } 		
    	 }
    	
    });
    
    
    /**
     * 新增优惠券;
     */
    $(doc).on("click", "#createXfsqActivityPage .btn-add-quan", function() {      
           if(createActivityPublicParam.voucherData.length == 5){
        	   return;
           }else{
        	   $("#createXfsqActivityPage #quan-sure").addClass("disabled");
               showPageWindow("createXfsqActivityPage","discountQuanDiv");
        	   createXfsqController.clearVoucherPageData(); 
           }
         
    });
    
    
    /**
     * 编辑优惠券;
     */
    $(doc).on("click", "#createXfsqActivityPage #voucherQuan", function(e) {
        if(e.target.className == "zhk-list_gb"){
        	return;
        }
        createXfsqController.showVoucherData($(this).attr("index_num"));
        showPageWindow("createXfsqActivityPage","discountQuanDiv");
    });
    
    /**
     * 删除优惠券;
     */
    $(doc).on('click','#createXfsqActivityPage .zhk-list_gb',function(e) {  
    	e.stopPropagation();
    	var _index = $(this).attr("index_num");
        createActivityPublicParam.voucherData.splice(_index,1);  	
    	createXfsqController.renderHtmlData();  
    	if(createActivityPublicParam.voucherData.length>0){
    		$("#createXfsqActivityPage #nextBtn").removeClass("disabled");
    	}else{
    		$("#createXfsqActivityPage #nextBtn").addClass("disabled");
    	}
    });
    
    
    /**
     * 优惠券取消;
     */
    $(doc).on("click", "#createXfsqActivityPage #quan-cancel , .layer-overlay", function() {      
    	closePageWindow("createXfsqActivityPage"); 
    });
    
    /**
     * 优惠券确认 ；
     */
    $(doc).on("click", "#createXfsqActivityPage #quan-sure", function() {
    	 if($(this).attr('class').indexOf("disabled")!=-1){
    		 return;
    	 }
    	 var receiveConditionMoney;
    	 if($("#createXfsqActivityPage #receiveConditionMoney").val()!=""){
    		 receiveConditionMoney =  parseFloat($("#createXfsqActivityPage #receiveConditionMoney").val()).toFixed(2);
    	 }
    	 if(!createXfsqController.checkReceiveCondition(receiveConditionMoney)){
    		 DialogUtil.dialogWarn("当前券的领取条件已经存在!");
    		 return;
    	 }
    	
         closePageWindow("createXfsqActivityPage");      
       	 var isAddQuan = ($("#createXfsqActivityPage .form-title").text().indexOf("添加")!=-1)?true:false; 
         if(isAddQuan==true){
             createXfsqController.saveVoucherData();
         }else{
        	 createXfsqController.saveVoucherData(XfsqConstants.voucherIndex);
         }

         createXfsqController.renderHtmlData();      
   
         $("#createXfsqActivityPage #nextBtn").removeClass("disabled");
   
         
    });

	$(doc).on('input', '#createXfsqActivityPage #voucherAmount,#createXfsqActivityPage #relativeValidDays,#createXfsqActivityPage #voucherMinConsumeAmount', function (e) {
		var reg = new RegExp("^[0-9]+\.{0,1}[0-9]{0,2}$"); 	 
    	var inputVal = $(this).val();
    	var inputId = $(this).attr("id");
    	if(inputVal == ""){
    		$(this).val("");
    	}else{
    		if(!reg.test(inputVal)){
        		var indexOfPoint =inputVal.indexOf(".");
            	if(indexOfPoint != -1){
            		if(inputVal.substring(indexOfPoint+1).length>2){
            			$(this).val(inputVal.substring(0,indexOfPoint+3));
            		}
            	} 
        	}else{//通过正则过滤;
        		if(inputVal.charAt(0)=='0'&& inputVal.charAt(1)!='' && inputVal.charAt(1)!='.'){
        			$(this).val(Number(inputVal));
        		}else if(inputId == "voucherAmount" && Number(inputVal) < 1){//代金券面额只能大于等于1;
        			$(this).val(1);
        		}
        	}
          	
    	}
	});
	
	//校验领取条件只能输入大于1正整数;
	$(doc).on('keypress', '#createXfsqActivityPage #receiveConditionMoney', function (e) {
		if($(this).val().length==0){
			return /^[1-9]+$/.test(String.fromCharCode(e.keyCode));
		}else{
			return /^[0-9]+$/.test(String.fromCharCode(e.keyCode));
		}
	});
 
    
    /**
     * 优惠图片上传事件;
     */
    $(doc).on("click","#createXfsqActivityPage #quan-pic",function(){
    	//暂不实现;
    	alert("暂未实现图片上传!");
    });
    
      
    
    /**
     * 选择代金券有效日期类型;
     */
    $(doc).on("click", "#createXfsqActivityPage #quan-deadline.switch-label", function() {
    	if($("#createXfsqActivityPage .actions-modal-button").size()>0){
			//应该关闭模态窗口(具体怎么关由伟胜提供方法)
			$.closeModal($("#createXfsqActivityPage .actions-modal"));
		}
    	 var _text = $(this).children("span");
         var _self = $(this);
         var buttons1 = [
             {
                 text: '相对时间',
                 onClick: function () {
                     _text.text("相对时间");
                     _self.siblings("p").hide().eq(0).show();
                 	createActivityPublicParam.isRelativeTime = true;
                 	createActivityPublicParam.autoDelayFlag = 'Y';//关闭自动续期;	
                 }
             },
             {
                 text: '指定时间',
                 onClick: function () {  
                	 if(createActivityPublicParam.autoDelayFlag == "Y"){
                      	 common.confirm({
 							"title":"",
 							"content": "选择指定时间，将会关闭自动续期功能，确定要选择指定时间吗?",
 							"btnRight": "确定",
 							"btnLeft": "取消",
 							 callbackLeft: function () {//取消
 								return false;
 							},
 							 callbackRight: function () {//确定
 								_text.text("指定时间");
 								_self.siblings("p").hide().eq(1).show();
 								createActivityPublicParam.autoDelayFlag = 'N';//关闭自动续期;		
 								createActivityPublicParam.isRelativeTime = false; 
 								return false;
 							}
 			           });      	 
                 	
                	 }else{
                		 _text.text("指定时间");
					     _self.siblings("p").hide().eq(1).show(); 
					     createActivityPublicParam.isRelativeTime = false; 
                	 }
          
                 }
             }
         ];
         var buttons2 = [
             {
                 text: '取消',
             }
         ];
         var groups = [buttons1, buttons2];
         $.actions(groups);
    });
    
   
     
})(document, window);